//
//  AddViewController.m
//  FMDBSingle
//
//  Created by qianfeng on 16/5/31.
//  Copyright © 2016年 qianfeng. All rights reserved.
//

#import "AddViewController.h"
#import "DatabaseManager.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height




@interface AddViewController ()

@property (nonatomic,strong)UITextField *nameField;
@property (nonatomic,strong)UITextField *ageField;
@property (nonatomic,strong)UITextField *sexField;


@property (nonatomic,strong)DatabaseManager *manager;

@end

@implementation AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createUI];
    [self createItems];

}

#pragma mark -- create UI
- (void)createUI{
    _nameField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, WIDTH - 200, 40)];
    _nameField.backgroundColor = [UIColor lightGrayColor];
    _nameField.borderStyle = UITextBorderStyleRoundedRect;
    
    _ageField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, WIDTH - 200, 40)];
    _ageField.backgroundColor = [UIColor lightGrayColor];
    _ageField.borderStyle = UITextBorderStyleRoundedRect;
    
    _sexField = [[UITextField alloc] initWithFrame:CGRectMake(100, 300, WIDTH - 200, 40)];
    _sexField.backgroundColor = [UIColor lightGrayColor];
    _sexField.borderStyle = UITextBorderStyleRoundedRect;
    
    if (_student) {
        _nameField.text = _student.stuName;
        _ageField.text = _student.stuAge;
        _sexField.text = _student.stuSex;
    }
    
    
    [self.view addSubview:_nameField];
    [self.view addSubview:_ageField];
    [self.view addSubview:_sexField];
}

- (void)createItems{
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemWasClick:)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemWasClick:)];
    
    [rightItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)leftItemWasClick:(UIBarButtonItem *)item{
    [self.navigationController popViewControllerAnimated:YES];
    
    if ([self.delegate respondsToSelector:@selector(sendMessage:andAge:andSex:)]) {
        [self.delegate sendMessage:_nameField.text andAge:_ageField.text andSex:_sexField.text];
        
        Student *stu = [[Student alloc]init];
        stu.stuName = _nameField.text;
        stu.stuAge = _ageField.text;
        stu.stuSex = _ageField.text;
        NSArray *arr = @[stu];
        [[DatabaseManager sharedManager] insertDataWithArray:arr];
        
        
        
    }
    
    }

- (void)rightItemWasClick:(UIBarButtonItem *)item{
    [self.navigationController popViewControllerAnimated:YES];
    

}

- (DatabaseManager *)manager{
    if (!_manager) {
        _manager = [[DatabaseManager alloc]init];
    }
    return _manager;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
